﻿#include "page_gg.h"
#include "ui_page_gg.h"
#include <cmath>

/*!
 *param [in] ryad - ряд распределения геометрического, гипергеометрического или особого распределения
 *возвращаемое значение - математические характеристики:
 *	- мат. ожидание
 *	- дисперсия
 *	- средне квадратичное отклонение
 */
std::vector<double> mathCharacteristics(std::vector<double> &ryad);

page_gg::page_gg(QWidget *parent)
	: page_clean(parent)
{
	ui = new Ui::page_ggClass();
	ui->setupUi(this);

    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_disp, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_f_raspr, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_sr_kv_otkl, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_math_o, SLOT(setChecked(bool)));
    QObject::connect(ui->checkBox, SIGNAL(clicked(bool)), ui->checkBox_rad_raspr, SLOT(setChecked(bool)));
}

page_gg::~page_gg()
{
    delete ui;
}

bool page_gg::inputHyperGeom(unsigned int *n, unsigned int *m, unsigned int *x)
{
    (*n) = ui->input_N->value();
    (*m) = ui->input_n->value();
    (*x) = ui->input_M->value();
    if( (*n) < (*m) || (*n) < (*x) || (*n)==0)
        return false;
    return true;
}

std::vector<double> page_gg::Rad(unsigned int n,unsigned int m,unsigned int x)
{
    std::vector<double> res;
	unsigned int max = std::min(m,x);
    for(unsigned int k=0;k<=max;++k)
    {
        res.push_back( (m_combinatorics(k,x).C() * m_combinatorics(m-k,n-x).C() / m_combinatorics(m,n).C()) );
    }
    return res;
}

void page_gg::on_calc_btn_clicked()
{
    unsigned int n;
    unsigned int m;
    unsigned int x;
    if(!inputHyperGeom(&n,&m,&x))
    {
        emit appendToLog("неверные данные");
        return;
    }
    std::vector<double> v = Rad(n,m,x);
    std::vector<double> params = mathCharacteristics(v);
    if(ui->checkBox_rad_raspr->checkState()==Qt::Checked)
    {
        show_Rad(v);
    }
    if(ui->checkBox_math_o->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("M(X) = ")+QString::number(params[0]));
    }
    if(ui->checkBox_disp->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("D(X) = ")+QString::number(params[1]));
    }
    if(ui->checkBox_sr_kv_otkl->checkState()==Qt::Checked)
    {
        emit appendToLog(QString("&#963;(X) = ")+QString::number(params[2]));
    }
    if(ui->checkBox_f_raspr->checkState()==Qt::Checked)
    {
        show_Graph(v);
    }
    //if(ui->checkBox_mng->checkState()==Qt::Checked)           //calc_Mng();
    //if(ui->checkBox_graph->checkState()==Qt::Checked)
    emit appendToLog("<hr />");
}
